package com.sxt;

import com.sxt.obj.BodyObj;
import com.sxt.obj.FoodObj;
import com.sxt.obj.HeadObj;
import com.sxt.utils.GameUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

public class GameWin extends JFrame {

    // 游戏状态 0.未开始  1.游戏中  2.暂停  3.失败  4.通关 5.失败后重新开始 6.下一关
    public static int state = 0;
    // 分数
    public int score = 0;
    // 当前关卡
    public int level = 1;

    // 蛇的速度
    public int speed = 1000;

    // 定义双缓存图片
    Image offScreenImage = null;
    // 窗口宽高
    int winWidth = 800;
    int winHeight = 600;
    // 蛇头对象
    public HeadObj headObj = new HeadObj(GameUtils.rightImg, 60, 570, this);
    // 蛇的身体集合
    public List<BodyObj> bodyObjList = new ArrayList<>();
    // 食物集合
    public List<FoodObj> foodObjList = new ArrayList<>();

    public void launch() {
        // 设置窗口是否可见
        this.setVisible(true);
        // 设置窗口的大小
        this.setSize(winWidth, winHeight);
        // 设置窗口的位置在屏幕上居中
        this.setLocationRelativeTo(null);
        // 设置窗口的标题
        this.setTitle("贪吃蛇小游戏");

        // 蛇身体的初始化
        bodyObjList.add(new BodyObj(GameUtils.bodyImg, 30, 570, this));
        bodyObjList.add(new BodyObj(GameUtils.bodyImg, 0, 570, this));
        // 初始化食物
        foodObjList.addAll(new FoodObj().getMultipleFood(level + 1));

        // 键盘事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    switch (state) {
                        case 0:
                            state = 1;
                            break;
                        case 1:
                            state = 2;
                            repaint();
                            break;
                        case 2:
                            state = 1;
                            break;
                        case 3:
                            state = 5;
                            break;
                        case 4:
                            state = 6;
                            break;
                        default:
                            break;
                    }
                }
            }
        });

        while (true) {
            if (state == 1) {
                repaint();
            }
            if (state == 5) {
                state = 0;
                resetGame();
            }
            if (state == 6 && GameUtils.level != 3) {
                state = 1;
                GameUtils.level++;
                level++;
//                加速
                speed -= 300;
                resetGame();
            }
            try {
                Thread.sleep(speed);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(winWidth, winHeight);
        }
        Graphics gImage = offScreenImage.getGraphics();
        gImage.setColor(Color.gray);
        gImage.fillRect(0, 0, winWidth, winHeight);
        gImage.setColor(Color.black);
        for (int i = 0; i <= 20; i++) {
            gImage.drawLine(0, i * 30, 600, i * 30);
            gImage.drawLine(i * 30, 0, i * 30, 600);
        }
        for (BodyObj body : bodyObjList) {
            body.paintSelf(gImage);
        }
        headObj.paintSelf(gImage);
        for (FoodObj food : foodObjList) {
            food.paintSelf(gImage);
        }
        GameUtils.drawWord(gImage, "第" + GameUtils.level + "关", Color.orange, 40, 650, 260);
        GameUtils.drawWord(gImage, score + " 分", Color.BLUE, 50, 650, 330);
        gImage.setColor(Color.gray);
        prompt(gImage);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    void prompt(Graphics g) {
        if (state == 0) {
            g.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(g, "按下空格键开始游戏", Color.yellow, 35, 150, 290);
        }
        if (state == 2) {
            g.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(g, "按下空格键继续游戏", Color.yellow, 35, 150, 290);
        }
        if (state == 3) {
            g.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(g, "失败,按空格重新开始", Color.red, 35, 150, 290);
        }
        if (state == 4) {
            g.fillRect(120, 240, 400, 70);
            if (GameUtils.level == 3) {
                GameUtils.drawWord(g, "达成条件,游戏通关", Color.green, 35, 150, 290);
            } else {
                GameUtils.drawWord(g, "达成条件,空格下一关", Color.green, 35, 150, 290);
            }
        }
    }

    void resetGame() {
        // 重置游戏状态
        score = 0;
        bodyObjList.clear();
        foodObjList.clear();
        headObj = new HeadObj(GameUtils.rightImg, 60, 570, this);

        // 蛇身体的初始化
        bodyObjList.add(new BodyObj(GameUtils.bodyImg, 30, 570, this));
        bodyObjList.add(new BodyObj(GameUtils.bodyImg, 0, 570, this));
        // 初始化食物
        foodObjList.addAll(new FoodObj().getMultipleFood(level + 1));
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
